//
//  LGBaseView.swift
//  01-Weibo项目练习-1
//
//  Created by 李李刚 on 16/3/8.
//  Copyright © 2016年 李李刚. All rights reserved.
//

import UIKit

import AFNetworking

//使用代理
@objc protocol LGBaseViewDelegate:NSObjectProtocol{
    
    optional func buttonDidClickView(baseView:LGBaseView , ClickButton:UIButton);
 
}








class LGBaseView: UIView {
    
    
    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
//        backgroundColor = UIColor.redColor()
        
        
        //加载控件
        setupUI()
        
//        startAnimation()
        
//        self.backgroundColor = UIColor(red: 237/255, green: 237/255, blue: 237/255, alpha: 1)

//        self.backgroundColor = UIColor.redColor()
        
        
        
        
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //懒加载三个控件
    //图标控件
    
    weak var delegate:LGBaseViewDelegate?
    
    lazy var iconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //圆圈控件
    lazy var circleView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //文字控件
    lazy var labelView = UILabel()
    
    //登录按钮
    private lazy var logButton: UIButton = {
        
        let button = UIButton()
        
        button.setTitleColor(UIColor.grayColor(), forState: .Normal)
        
        button.setBackgroundImage(UIImage(named: "common_button_white"), forState: .Normal)
        //添加点击事件
        button.addTarget(self, action: "logButtonDidClick:", forControlEvents: .TouchUpInside)
        
        //设置文字
        button.setTitle("登录", forState: .Normal)
        
        return button
        
        
    }()
    
   @objc private func logButtonDidClick(button:UIButton){
        
        print("登录按钮点击")
        
        delegate?.buttonDidClickView?(self, ClickButton: button)
    
    }
    
    //开启转圈动画
    func startAnimation(){
        
        let anima = CABasicAnimation(keyPath: "transform.rotation")
        
        anima.toValue = 2 * M_PI
        
        anima.repeatCount = MAXFLOAT

        
        anima.duration = 10
        
        
        //在切换页面或者应用退到后台之后,动画会停止
        anima.removedOnCompletion = false
        
        circleView.layer.addAnimation(anima, forKey: "circle")
        
        
    }
    
    
    
    
    
    //懒加载注册按钮
    private lazy var registButton: UIButton = {
       
        let button = UIButton()
        
        button.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        
        button.setBackgroundImage(UIImage(named: "common_button_white"), forState: .Normal)
        //添加点击事件
        button.addTarget(self, action: "registButtonDidClick:", forControlEvents: .TouchUpInside)
        
        //设置文字
        button.setTitle("注册", forState: .Normal)
        
        return button
        
        
    }()
    
    //注册按钮点击事件
 @objc  private func registButtonDidClick(button:UIButton){
        
        print("注册按钮点击")
        
        delegate?.buttonDidClickView?(self, ClickButton: button)

        
    }
    
    
    //懒加载遮罩控件
    private lazy var maskIconView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    
    
    
    
    
    func setupUI(){
        
        self.backgroundColor = UIColor(red: 237/255, green: 237/255, blue: 237/255, alpha: 1)
        
        
        
        addSubview(circleView)
        addSubview(maskIconView)
        addSubview(iconView)
        
        addSubview(labelView)
        addSubview(logButton)
        addSubview(registButton)
        
        
        
        //开启约数设置
        iconView.translatesAutoresizingMaskIntoConstraints = false
        //添加约数
        addConstraint(NSLayoutConstraint(item:iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        //圈的约数
        circleView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        
        
        //label约数设置
        labelView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: labelView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: circleView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 5))
        
        addConstraint(NSLayoutConstraint(item: labelView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 224))
        
        addConstraint(NSLayoutConstraint(item: labelView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        
        labelView.text = "哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈"
        
        labelView.textAlignment = .Center
        
        labelView.numberOfLines = 0
        
        labelView.textColor = UIColor.grayColor()
        
        labelView.font = UIFont.systemFontOfSize(14)
        
        
        //注册按钮约数
        registButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: labelView, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: labelView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 15))
        addConstraint(NSLayoutConstraint(item: registButton, attribute: .Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        
        //登录按钮约数设置
        logButton.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: logButton, attribute: .Trailing, relatedBy: .Equal, toItem: labelView, attribute: .Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: logButton, attribute: .Top, relatedBy: .Equal, toItem: registButton, attribute: .Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: logButton, attribute: .Width, relatedBy: .Equal, toItem: registButton, attribute: .Width, multiplier: 1, constant: 0))
        
        //遮罩控件约数
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
        
        
        
        
        startAnimation()
        
        
    }
    

    //设置访客视图内容的方法
    func setpageinfo(imageName:String? , message:String?){
        
        //如果为空,代表是首页
        if imageName == nil {
            
            startAnimation()
            
        }else{
            
            circleView.hidden = true
            
            iconView.image = UIImage(named: imageName!)
            
            labelView.text = message
        }
        
        
    }
    
    



}


//extension LGBaseView{
//    
//    private func setcolor(){
//        
//        
//        self.backgroundColor = UIColor(red: 237/255, green: 237/255, blue: 237/255, alpha: 1)
//        
//    }
//    
//  
//    
//}
